
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java 基础 -- StringBuilder 和 StringBuffer · 寒江蓑笠的博客</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="介绍 StringBuilder 和 StringBuffer 的用法">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="java-basic-knowledge-about-string.html" />
    
    
    <link rel="prev" href="java-basic-knowledge-parameter-passing.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="java-basic-joor.html">
            
                <a href="java-basic-joor.html">
            
                    
                    Java 基础 -- 使用 JOOR 实现反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="java-basic-annotation.html">
            
                <a href="java-basic-annotation.html">
            
                    
                    Java基础 -- 注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="java-basic-threadlocal.html">
            
                <a href="java-basic-threadlocal.html">
            
                    
                    Java 基础 -- ThreadLocal
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="java-development-skills-get-stacktrace.html">
            
                <a href="java-development-skills-get-stacktrace.html">
            
                    
                    Java 开发技巧 -- 获取 Java 的调用栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="java-basic-concurrent-map-collection.html">
            
                <a href="java-basic-concurrent-map-collection.html">
            
                    
                    Java 基础 -- 并发集合的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="java-dynamic-proxy-invocationhandler-demo.html">
            
                <a href="java-dynamic-proxy-invocationhandler-demo.html">
            
                    
                    Java InvocationHandler 实现动态代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.7" data-path="java-basic-try-catch-finally.html">
            
                <a href="java-basic-try-catch-finally.html">
            
                    
                    Java 基础 -- 深入理解 try catch finally
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8" data-path="java-basic-reflect.html">
            
                <a href="java-basic-reflect.html">
            
                    
                    Java 基础 -- 反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.9" data-path="java-basic-io-stream.html">
            
                <a href="java-basic-io-stream.html">
            
                    
                    Java IO -- IO流的概念和分类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.10" data-path="java-basic-final.html">
            
                <a href="java-basic-final.html">
            
                    
                    Java 基础 -- 深入理解 final 修饰符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.11" data-path="java-basic-memory-manage-constructor.html">
            
                <a href="java-basic-memory-manage-constructor.html">
            
                    
                    Java 内存管理 -- 父子实例的内存控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.12" data-path="java-basic-memory-manage-inherit-object.html">
            
                <a href="java-basic-memory-manage-inherit-object.html">
            
                    
                    Java 内存管理 -- 构造方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.13" data-path="java-basic-memory-manage-memory-area.html">
            
                <a href="java-basic-memory-manage-memory-area.html">
            
                    
                    Java 内存管理 -- 内存区域的划分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.14" data-path="java-basic-memory-manage-variable.html">
            
                <a href="java-basic-memory-manage-variable.html">
            
                    
                    Java 内存管理 -- 变量的内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.15" data-path="java-basic-knowledge-generic.html">
            
                <a href="java-basic-knowledge-generic.html">
            
                    
                    Java 基础 -- 范型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.16" data-path="java-basic-knowledge-auto-boxing.html">
            
                <a href="java-basic-knowledge-auto-boxing.html">
            
                    
                    Java 基础 -- 关于自动装箱和拆箱机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.17" data-path="java-basic-knowledge-parameter-passing.html">
            
                <a href="java-basic-knowledge-parameter-passing.html">
            
                    
                    Java 基础 -- 值传递、引用传递和参数传递
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.1.18" data-path="java-basic-stringbuilder-stringbuffer.html">
            
                <a href="java-basic-stringbuilder-stringbuffer.html">
            
                    
                    Java 基础 -- StringBuilder 和 StringBuffer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.19" data-path="java-basic-knowledge-about-string.html">
            
                <a href="java-basic-knowledge-about-string.html">
            
                    
                    Java 基础 -- 深入理解 String
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.20" data-path="java-basic-linkedhashmap.html">
            
                <a href="java-basic-linkedhashmap.html">
            
                    
                    Java 基础 -- LinkedHashMap 的使用和原理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Java 基础 -- StringBuilder 和 StringBuffer</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6982;&#x8FF0;">&#x6982;&#x8FF0;</h2>
<p>StringBuilder &#x548C; StringBuffer &#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x662F;&#x6211;&#x4EEC;&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#x7684;&#x7C7B;&#xFF0C;&#x5728;<a href="http://www.heqiangfly.com/2014/01/20/java-basic-knowledge-about-string/" target="_blank">Java &#x57FA;&#x7840; -- &#x6DF1;&#x5165;&#x7406;&#x89E3; String </a> &#x7B80;&#x5355;&#x4ECB;&#x7ECD;&#x8FC7;&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x3002;&#x4E0E; String &#x7C7B;&#x4E0D;&#x540C;&#xFF0C;StringBuffer &#x548C; StringBuilder &#x7C7B;&#x7684;&#x5BF9;&#x8C61;&#x80FD;&#x591F;&#x88AB;&#x591A;&#x6B21;&#x7684;&#x4FEE;&#x6539;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x4EA7;&#x751F;&#x65B0;&#x7684;&#x672A;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5728;&#x67D0;&#x4E9B;&#x573A;&#x666F;&#x4E0B;&#x4F7F;&#x7528;StringBuilder &#x548C; StringBuffer&#x80FD;&#x591F;&#x4F18;&#x5316;&#x6211;&#x4EEC;&#x7A0B;&#x5E8F;&#x7684;&#x5185;&#x5B58;&#x548C;&#x6027;&#x80FD;&#x3002;
StringBuilder &#x548C; StringBuffer &#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x90FD;&#x662F;&#x88AB; final &#x4FEE;&#x9970;&#x7684;&#xFF0C;&#x5B83;&#x4EEC;&#x662F;&#x4E0D;&#x80FD;&#x88AB;&#x7EE7;&#x627F;&#x7684;&#x3002;
&#x9996;&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x4ED6;&#x4EEC;&#x7684;&#x7C7B;&#x5173;&#x7CFB;&#x56FE;&#xFF1A;</p>
<p><img src="../../images/java-basic-stringbuilder-stringbuffer/class.png" alt="&#x6548;&#x679C;&#x56FE;"></p>
<h2 id="&#x65B9;&#x6CD5;">&#x65B9;&#x6CD5;</h2>
<ul>
<li>append()&#xFF1A;&#x5C06;&#x6307;&#x5B9A;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8FFD;&#x52A0;&#x5230;&#x6B64;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x3002;</li>
<li>appendCodePoint()&#xFF1A;&#x5C06; codePoint&#x53C2;&#x6570;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x793A;&#x6CD5;&#x9644;&#x52A0;&#x5230;&#x6B64;&#x5E8F;&#x5217;&#x3002;&#x6BD4;&#x5982;&#x8FFD;&#x52A0;&#x4E00;&#x4E9B;ascii&#x7801;&#x8868;&#x793A;&#x7684;&#x5B57;&#x7B26;&#x6216;&#x8005;&#x8868;&#x60C5;&#x7B26;&#x53F7;emoji&#x7B49;&#x3002;</li>
<li>capacity()&#xFF1A;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x5BB9;&#x91CF;&#x3002;</li>
<li>charAt(int index)&#xFF1A;&#x8FD4;&#x56DE;&#x6B64;&#x5E8F;&#x5217;&#x4E2D;&#x6307;&#x5B9A;&#x7D22;&#x5F15;&#x5904;&#x7684; char &#x503C;&#x3002;</li>
<li>codePointAt(int index)&#xFF1A;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x5B57;&#x7B26;&#x7684;Unicode code point&#x3002;&#x548C;String&#x7C7B;&#x4F3C;&#x3002;</li>
<li>codePointBefore(int index)&#xFF1A;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x4E4B;&#x524D;&#x5B57;&#x7B26;&#x7684;Unicode code point&#x3002;&#x548C;String&#x7C7B;&#x4F3C;&#x3002;</li>
<li>codePointCount(int index)&#xFF1A;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x4E4B;&#x540E;&#x5B57;&#x7B26;&#x7684;Unicode code point&#x3002;&#x548C;String&#x7C7B;&#x4F3C;&#x3002;</li>
<li>delete()&#xFF1A;&#x79FB;&#x9664;&#x6B64;&#x5E8F;&#x5217;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5B57;&#x7B26;&#x3002;</li>
<li>deleteCharAt()&#xFF1A;&#x79FB;&#x9664;&#x6B64;&#x5E8F;&#x5217;&#x67D0;&#x4E2A;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x7684;&#x5B57;&#x7B26;&#x3002;</li>
<li>ensureCapacity(int minimumCapacity)&#xFF1A;&#x786E;&#x4FDD;&#x5BB9;&#x91CF;&#x81F3;&#x5C11;&#x7B49;&#x4E8E;&#x6307;&#x5B9A;&#x7684;&#x6700;&#x5C0F;&#x503C;&#x3002;</li>
<li>getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)&#xFF1A;&#x5C06;&#x5B57;&#x7B26;&#x4ECE;&#x6B64;&#x5E8F;&#x5217;&#x590D;&#x5236;&#x5230;&#x76EE;&#x6807;&#x5B57;&#x7B26;&#x6570;&#x7EC4; dst&#x3002;</li>
<li>indexOf()&#xFF1A;&#x8FD4;&#x56DE;&#x7B2C;&#x4E00;&#x6B21;&#x51FA;&#x73B0;&#x7684;&#x6307;&#x5B9A;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x5728;&#x8BE5;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x7D22;&#x5F15;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x5C31;&#x8FD4;&#x56DE; -1&#x3002;</li>
<li>insert()&#xFF1A;&#x5C06;&#x6307;&#x5B9A;&#x6570;&#x636E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x793A;&#x5F62;&#x5F0F;&#x63D2;&#x5165;&#x6B64;&#x5E8F;&#x5217;&#x4E2D;&#x3002;</li>
<li>lastIndexOf()&#xFF1A;&#x8FD4;&#x56DE;&#x6700;&#x53F3;&#x8FB9;&#x51FA;&#x73B0;&#x7684;&#x6307;&#x5B9A;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x5728;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x7D22;&#x5F15;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x5C31;&#x8FD4;&#x56DE; -1&#x3002;</li>
<li>length()&#xFF1A;&#x8FD4;&#x56DE;&#x957F;&#x5EA6;&#xFF08;&#x5B57;&#x7B26;&#x6570;&#xFF09;&#x3002;</li>
<li>offsetByCodePoints(int index, int codePointOffset)&#xFF1A;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;index&#x4F4D;&#x7F6E;&#x504F;&#x79FB; codePointOffset &#x5904;&#x5B57;&#x7B26;&#x7684;Unicode code point&#x3002;&#x548C;String&#x7C7B;&#x4F3C;&#x3002;</li>
<li>readObject(ObjectInputStream s)&#xFF1A;&#x5C06;StringBuffer &#x6216; StringBuffer&#x5BF9;&#x8C61; &#x4ECE;ObjectInputStream &#x5BF9;&#x8C61;&#x4E2D;&#x6062;&#x590D;&#xFF0C;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x3002;</li>
<li>writeObject(ObjectOutputStream s)&#xFF1A;&#x5C06;&#x5F53;&#x524D;StringBuffer &#x6216; StringBuffer&#x5BF9;&#x8C61;&#x4FDD;&#x5B58;&#x5230; ObjectOutputStream &#x5BF9;&#x8C61;&#xFF0C;&#x5E8F;&#x5217;&#x5316;&#x3002;</li>
<li>replace()&#xFF1A;&#x4F7F;&#x7528;&#x7ED9;&#x5B9A; String &#x4E2D;&#x7684;&#x5B57;&#x7B26;&#x66FF;&#x6362;&#x6B64;&#x5E8F;&#x5217;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5B57;&#x7B26;&#x3002;</li>
<li>reverse()&#xFF1A;&#x5C06;&#x6B64;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x7528;&#x5176;&#x53CD;&#x8F6C;&#x5F62;&#x5F0F;&#x53D6;&#x4EE3;&#x3002;</li>
<li>setCharAt(int index, char ch)&#xFF1A;&#x5C06;&#x7ED9;&#x5B9A;&#x7D22;&#x5F15;&#x5904;&#x7684;&#x5B57;&#x7B26;&#x8BBE;&#x7F6E;&#x4E3A; ch&#x3002;</li>
<li>setLength(int newLength)&#xFF1A;&#x8BBE;&#x7F6E;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x7684;&#x957F;&#x5EA6;&#x3002;</li>
<li>subSequence(int start, int end)&#xFF1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#xFF0C;&#x8BE5;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x662F;&#x6B64;&#x5E8F;&#x5217;&#x7684;&#x5B50;&#x5E8F;&#x5217;&#x3002;</li>
<li>substring()&#xFF1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684; String&#xFF0C;&#x5B83;&#x5305;&#x542B;&#x6B64;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x5F53;&#x524D;&#x6240;&#x5305;&#x542B;&#x7684;&#x5B57;&#x7B26;&#x5B50;&#x5E8F;&#x5217;&#x3002;</li>
<li>toString()&#xFF1A;&#x8F6C;&#x6362;&#x6210; Sting &#x5BF9;&#x8C61;&#x3002;</li>
<li>trimToSize()&#xFF1A;&#x5C06;StringBuffer &#x6216; StringBuffer &#x5BF9;&#x8C61;&#x7684;&#x4E2D;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#x7F29;&#x5C0F;&#x5230;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;&#x4E00;&#x6837;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x51CF;&#x5C11;&#x7A7A;&#x95F4;&#x7684;&#x6D6A;&#x8D39;&#x3002;</li>
</ul>
<h2 id="&#x533A;&#x522B;">&#x533A;&#x522B;</h2>
<p>&#x4E24;&#x8005;&#x7684;&#x65B9;&#x6CD5;&#x57FA;&#x672C;&#x76F8;&#x540C;&#x3002;&#x533A;&#x522B;&#x5C31;&#x662F; StringBulider &#x662F;&#x975E;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;&#xFF0C;StringBuffer &#x662F;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;&#x3002;&#x901A;&#x8FC7;&#x770B;&#x6E90;&#x7801;&#x53D1;&#x73B0; StringBuffer &#x7684;&#x65B9;&#x6CD5;&#x57FA;&#x672C;&#x90FD;&#x4F7F;&#x7528;&#x4E86; synchronized &#x5173;&#x952E;&#x5B57;&#x6765;&#x4FDD;&#x8BC1;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x3002;
&#x5728;&#x5355;&#x7EBF;&#x7A0B;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;StringBulider &#x6BD4; StringBuffer &#x6548;&#x7387;&#x66F4;&#x9AD8;&#xFF0C;&#x56E0;&#x4E3A;&#x7701;&#x53BB;&#x4E86;&#x52A0;&#x9501;&#x7684;&#x5F00;&#x9500;&#x3002;</p>
<h2 id="&#x5B9E;&#x73B0;&#x539F;&#x7406;">&#x5B9E;&#x73B0;&#x539F;&#x7406;</h2>
<p>StringBulider &#x6BD4; StringBuffer &#x90FD;&#x662F;&#x7EE7;&#x627F;&#x81EA; AbstractStringBuilder &#x7C7B;&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x5C31;&#x5206;&#x6790;&#x4E00;&#x4E0B; AbstractStringBuilder &#x7684;&#x5B9E;&#x73B0;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x5B83;&#x4EEC;&#x7684;&#x539F;&#x7406;&#x3002;
AbstractStringBuilder &#x5185;&#x90E8;&#x7528; <code>char[] value</code> &#x53D8;&#x91CF;&#x7C7B;&#x5B58;&#x50A8;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#xFF0C;<code>int count</code> &#x53D8;&#x91CF;&#x6765;&#x6807;&#x8BC6;&#x5B9E;&#x9645;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x3002;
&#x5728;&#x521D;&#x59CB;&#x5316; StringBulider &#x6BD4; StringBuffer &#x65F6;&#x5982;&#x679C;&#x4E0D;&#x6307;&#x5B9A;&#x5BB9;&#x91CF;&#x503C;&#xFF0C;&#x90A3;&#x4E48; <code>char[]</code> &#x6570;&#x7EC4;&#x9ED8;&#x8BA4;&#x5927;&#x5C0F;&#x662F;16&#x3002;&#x90A3;&#x4E48;&#x5728;&#x6211;&#x4EEC;&#x4E0D;&#x65AD;&#x7684; append &#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x6570;&#x7EC4;&#x7684;&#x5BB9;&#x91CF;&#x80AF;&#x5B9A;&#x4F1A;&#x6709;&#x4E0D;&#x591F;&#x7528;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#x3002;</p>
<h3 id="&#x6269;&#x5BB9;">&#x6269;&#x5BB9;</h3>
<p>&#x5728; append &#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;&#x5728;&#x8FDB;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x62FC;&#x63A5;&#x4E4B;&#x524D;&#x90FD;&#x4F1A;&#x8C03;&#x7528; <code>ensureCapacityInternal</code> &#x65B9;&#x6CD5;&#x68C0;&#x67E5;&#x5F53;&#x524D;&#x6570;&#x7EC4;&#x662F;&#x5426;&#x9700;&#x8981;&#x6269;&#x5BB9;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x6269;&#x5BB9;&#x7684;&#x5C31;&#x8981;&#x8C03;&#x7528; <code>expandCapacity</code> &#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#xFF1A;</p>
<pre><code>    void expandCapacity(int minimumCapacity) {
        int newCapacity = value.length * 2 + 2;
        if (newCapacity - minimumCapacity &lt; 0)
            newCapacity = minimumCapacity;
        if (newCapacity &lt; 0) {
            if (minimumCapacity &lt; 0) // overflow
                throw new OutOfMemoryError();
            newCapacity = Integer.MAX_VALUE;
        }
        value = Arrays.copyOf(value, newCapacity);
    }
</code></pre><p>&#x6BCF;&#x6B21;&#x7684;&#x6269;&#x5BB9;&#x4F1A;&#x8C03;&#x7528;Arrays.copyOf&#x91CD;&#x65B0;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x539F;&#x6765;2&#x500D;+2&#x5927;&#x5C0F;&#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x7136;&#x540E;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8D4B;&#x503C;&#x5230;&#x539F;&#x6765;&#x7684;&#x6570;&#x7EC4;&#x3002;&#x8FD9;&#x671F;&#x95F4;&#x4F34;&#x968F;&#x7740;&#x5185;&#x5B58;&#x7684;&#x7533;&#x8BF7;&#x548C;&#x56DE;&#x6536;&#xFF0C;&#x5982;&#x679C;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x8F83;&#x5927;&#xFF0C;&#x5BF9;&#x6027;&#x80FD;&#x4F1A;&#x9020;&#x6210;&#x5F88;&#x5927;&#x7684;&#x5E94;&#x7528;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x6784;&#x9020; StringBulider &#x6BD4; StringBuffer &#x65F6;&#x8981;&#x5408;&#x7406;&#x9884;&#x4F30;&#x4F7F;&#x7528;&#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x5408;&#x7406;&#x8BBE;&#x7F6E;&#x521D;&#x59CB;&#x5BB9;&#x91CF;&#x5927;&#x5C0F;&#x3002;</p>
<!--

@startuml
Title "StringBuilder 和 StringBuffer类图" 
interface CharSequence
interface Serializable
interface Appendable
abstract class AbstractStringBuilder
class StringBuilder


CharSequence <|.. AbstractStringBuilder
CharSequence <|.. StringBuilder
Serializable <|.. StringBuilder
Appendable <|.. StringBuilder
AbstractStringBuilder <|-- StringBuilder
CharSequence <|.. StringBuffer
Serializable <|.. StringBuffer
Appendable <|.. StringBuffer
AbstractStringBuilder <|-- StringBuffer
@enduml
-->
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="java-basic-knowledge-parameter-passing.html" class="navigation navigation-prev " aria-label="Previous page: Java 基础 -- 值传递、引用传递和参数传递">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="java-basic-knowledge-about-string.html" class="navigation navigation-next " aria-label="Next page: Java 基础 -- 深入理解 String">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java 基础 -- StringBuilder 和 StringBuffer","categories":"Java","comments":true,"tags":["Java"],"description":"介绍 StringBuilder 和 StringBuffer 的用法","date":"2014-01-22T10:00:00.000Z","level":"1.1.18","depth":2,"next":{"title":"Java 基础 -- 深入理解 String","level":"1.1.19","depth":2,"path":"source/Java/java-basic-knowledge-about-string.md","ref":"source/Java/java-basic-knowledge-about-string.md","articles":[]},"previous":{"title":"Java 基础 -- 值传递、引用传递和参数传递","level":"1.1.17","depth":2,"path":"source/Java/java-basic-knowledge-parameter-passing.md","ref":"source/Java/java-basic-knowledge-parameter-passing.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"寒江蓑笠的博客","gitbook":"*"},"file":{"path":"source/Java/java-basic-stringbuilder-stringbuffer.md","mtime":"2022-05-17T10:49:59.390Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-17T10:54:58.084Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

